package com.edu.web.servlet;

import com.alibaba.fastjson2.JSON;
import com.edu.entity.User;
import com.edu.service.UserService;
import com.edu.service.imlp.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/registerUserServlet")
public class RegisterUserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取信息
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        //封装user
        User u = new User();
        u.setUsername(username);
        u.setPassword(password);
        u.setName(name);
        u.setEmail(email);
        u.setPhone(phone);
        u.setState(1);
        System.out.println(u);
        //添加
        int i = userService.addUser(u);
        String meg = "";
        if(i== 1){
            meg="注册成功";
        }else {
            meg="注册失败";
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("meg", meg);
        String jsonString = JSON.toJSONString(map);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonString);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
